//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
	data: {
		latitude: "",
		longitude: "",
		addressName: null,
		searchAddress:null,
		placeName:"",
	},
	bindKeyInput:function(e){
		var value = e.detail.value;
		this.setData({
			placeName: value
		});
	},
	bindButtonTap: function() {
		//查询坐标
		this.getAddress(this.data.placeName);
	},
	onLoad: function(options) {
		this.setData({
			addressName: options.adJson
		});
		this.getAddress(this.data.addressName);
	},
	getAddress:function(name){
		let _this = this;
		api.getLocationByName({
			data: {
				userId: app.globalData.userId,
				placeName:name,
			},
			success: function(res) {
				console.log(res.data.msg);
				_this.setData({
					searchAddress: JSON.parse(res.data.msg)
				});
			}
		});
	},
	resetAddress: function() {
		let _this = this;
		app.getLocation(function(latitude, longitude) {
			_this.getSiteName(longitude, latitude);
		});
	},
	//定位当前位置
	getSiteName(lng, lat) {
		let _this = this;
		api.getSiteName({
			data: {
				userId: app.globalData.userId,
				lng: lng,
				lat: lat
			},
			success: function(res) {
				let adJson = JSON.parse(res.data.msg);
				_this.setData({
					addressName: adJson.result.formatted_address,
					latitude: lat,
					longitude: lng,
				});
			}
		});
	},
	searchName:function(){
		//返回前一页
		wx.navigateBack({
			delta:1
		})
	},
	//地址查询
	searchCrouse: function(event) {
		let _this = this;
		let address = event.currentTarget.dataset.address;
		var pages = getCurrentPages();
		var prevPage = pages[pages.length - 2];  //上一个页面
		//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
		prevPage.setData({
			addressName:address.name,
			longitude:address.location.lng,
			latitude:address.location.lat,
		})
		//返回前一页
		wx.navigateBack({
			delta:1
		})
	}

})